// pages/components/authorize/authorize.js
var req = require("../../utils/action.js");
var utils = require("../../utils/util.js");
const app = getApp()
Component({
  properties: {
    userInfo: { // 属性名
      type: Map,
      value: null
    }
  },
  observers:{
    "userInfo":function(userInfo){

    }
  },
  ready: function() { 
    console.log(this.properties.userInfo)
  },
  data: {

  },
  methods: {
    getPhoneNumber:function(e){
      console.log(this.properties.userInfo);
      let userInfo = this.properties.userInfo
      if(this.properties.userInfo == null){
        userInfo = getApp().globalData.userInfo
      }
      if(e.detail.errMsg == 'getPhoneNumber:ok'){
        let r = {
          customerId:userInfo.id||wx.getStorageSync('customerId'),
          encryptedData:e.detail.encryptedData,iv:e.detail.iv,
          sessionKey:wx.getStorageSync('sessionKey')}
        let _url = req.actions.ym+"customer/getPhoneNumber";
        let that = this;
        utils.myResquest(r,_url,function(res){
          console.log(res.data)
          if(res.data.code == '200'){
            let phoneNum = res.data.content
            wx.setStorageSync('phone', phoneNum)
            if(app.globalData.userInfo){
              app.globalData.userInfo.phone = phoneNum
            }
            that.triggerEvent("myevent",{state:'success'})
          }else if(res.data.code == '406'){
            //登录过期
            app.login(function(res){
              if(res == 'complete'){
                console.log(res)
                utils.myResquest(r,_url,function(res){
                  console.log(res.data)
                  if(res.data.code == '200'){
                    let phoneNum = res.data.content
                    wx.setStorageSync('phone', phoneNum)
                    app.globalData.userInfo.phone = phoneNum
                    console.log("app.globalData.userInfo===>"+JSON.stringify(app.globalData.userInfo))
                    that.triggerEvent("myevent",{state:'success'})
                  }
                });
              }
            })
          }else{
            wx.showModal({
              title: '登陆异常'
            })
          }
        });
      }else{
        wx.showModal({
          title: '授权失败'
        })
      }
    }
  }
})
